import 'dart:convert';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:love_newf/app/com/base/L.dart';
import 'package:love_newf/app/com/manage/txim_manage/im_manage.dart';
import 'package:love_newf/app/com/model/login_user_info_model.dart';
import 'package:liuxlcommon/view/my_style.dart';
import 'package:love_newf/app/com/plugin/message_plugin.dart';
import 'package:love_newf/app/com/util/gift_utils.dart';
import 'package:love_newf/app/page/login/login_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'package:liuxlcommon/util/sp_utils.dart';
import 'package:liuxlcommon/view/loading_view.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:love_newf/generated/assets.dart';
import 'package:love_newf/value/app_colors.dart';
import 'package:love_newf/value/app_const.dart';

class App {
  static late BuildContext appContext;
  static String? token;
  static String localeCode = "en";

  static bool get localeEn => localeCode == "en";
  static LoginUserInfoModel userInfoModel = LoginUserInfoModel();

  static bool get sdkInitSuccess {
    if (ImManage().initSuccess &&
        LiveMessagePlugin.zegoInitSuccess &&
        LiveMessagePlugin.beautyInitSuccess) {
      return true;
    }
    return false;
  }

  ///页面加载之前
  static init() async {
    eazyLoading();
    await SpUtils.init();
    getUserAndToken();
    LoadingView.textColor = AppColors.textSecondColor;
  }

  ///用户必须登录,页面加载完成。
  static init2() async {
    settingLoadViewEmptyView();
    LiveMessagePlugin.zegoInit;
    ImManage().initIM();
      GiftUtils.roomPropList;
  }

  ///定义全局的 routeObserver 对象
  static final RouteObserver<PageRoute> routeObserver = RouteObserver();

  static getUserAndToken() {
    var json1 = SpUtils().getStorage("loginUserInfo");
    var token = SpUtils().getStorage("token");
    if (json1 != null && token != null) {
      App.token = token;
      userInfoModel = LoginUserInfoModel.fromJson(json.decode(json1));
    }
  }

  static bool isLogin() {
    return (L.loginUserInfo?.saToken ?? "").isNotEmpty;
  }

  static eazyLoading() {
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 2000)
      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
      ..loadingStyle = EasyLoadingStyle.custom
      ..maskType = EasyLoadingMaskType.custom
      ..indicatorSize = 45.0
      ..radius = 10.0
      ..backgroundColor = Colors.transparent
      ..indicatorColor = AppColors.primaryColor
      ..textColor = Colors.black
      ..progressColor = AppColors.primaryColor
      ..maskColor = Colors.black.withOpacity(0);
  }

  static logout() {
    L.reset();
    SpUtils().setStorage("loginUserInfo", null);
    Get.offAll(LoginPage());
  }

  static settingLoadViewEmptyView() {
    LoadingView.defaultEmptyView = Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          LImage.easy(Assets.imgEmpty,112.w,117.w),
          6.lsh,
          LText.easy('点击刷新', 14.sp, fw4, const Color(0xFFA1A1A1))
        ],
      ),
    );
    LoadingView.defaultErrorView = Center(
      child: LColumn(lStyle: LStyle(mainAxisSize: MainAxisSize.min), children: [
        // SizedBox(
        //   width: 209.w,
        //   child: Image.asset(Assets.icError),
        // ),
        LSizeBoxHeight(24),
        LText.easy(
            "点击重试", 14.sp, FontWeight.w600, AppColors.textSecondColor),
      ]),
    );
  }
}
